package com.sld.concrete.device.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;

import com.sld.concrete.device.domain.InfoDevice;
import com.sld.concrete.device.domain.RelateDeviceVehicle;
import com.sld.concrete.device.mapper.InfoDeviceMapper;
import com.sld.concrete.device.mapper.RelateDeviceVehicleMapper;
import com.sld.concrete.device.service.IInfoDeviceService;
import com.sld.concrete.vehicle.domain.InfoVehicle;
import com.sld.concrete.vehicle.mapper.InfoVehicleMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 设备管理Service业务层处理
 * 
 * @author topviewers
 * @date 2022-03-13
 */
@Service
public class InfoDeviceServiceImpl implements IInfoDeviceService
{
    @Resource
    private InfoDeviceMapper infoDeviceMapper;

    @Resource
    private InfoVehicleMapper vehicleMapper;

    @Resource
    RelateDeviceVehicleMapper relateDeviceVehicleMapper;


    /**
     * 查询设备管理
     * 
     * @param devId 设备管理主键
     * @return 设备管理
     */
    @Override
    public InfoDevice selectInfoDeviceByDevId(String devId)
    {
        return infoDeviceMapper.selectInfoDeviceByDevId(devId);
    }

    /**
     * 查询设备管理列表
     * 
     * @param infoDevice 设备管理
     * @return 设备管理
     */
    @Override
    public List<InfoDevice> selectInfoDeviceList(InfoDevice infoDevice)
    {
        return infoDeviceMapper.selectInfoDeviceList(infoDevice);
    }

    /**
     * 新增设备管理
     * 
     * @param infoDevice 设备管理
     * @return 结果
     */
    @Override
    public int insertInfoDevice(InfoDevice infoDevice)
    {
        infoDevice.setCreateTime(DateUtils.getNowDate());
        return infoDeviceMapper.insertInfoDevice(infoDevice);
    }

    /**
     * 修改设备管理
     * 
     * @param infoDevice 设备管理
     * @return 结果
     */
    @Override
    public int updateInfoDevice(InfoDevice infoDevice)
    {
        infoDevice.setUpdateTime(DateUtils.getNowDate());
        return infoDeviceMapper.updateInfoDevice(infoDevice);
    }

    /**
     * 批量删除设备管理
     * 
     * @param devIds 需要删除的设备管理主键
     * @return 结果
     */
    @Override
    public int deleteInfoDeviceByDevIds(String devIds)
    {
        return infoDeviceMapper.deleteInfoDeviceByDevIds(Convert.toStrArray(devIds));
    }

    /**
     * 删除设备管理信息
     * 
     * @param devId 设备管理主键
     * @return 结果
     */
    @Override
    public int deleteInfoDeviceByDevId(String devId)
    {
        return infoDeviceMapper.deleteInfoDeviceByDevId(devId);
    }

    @Override
    public int updateInfoDeviceBindPlate(String devId, String plateNo) throws Exception{
        InfoDevice device = infoDeviceMapper.selectInfoDeviceByDevId(devId);

        //看下车牌，如果没有录入就拒绝
        InfoVehicle infoVehicle = vehicleMapper.queryVehicleByPlateNo(plateNo);
        if (infoVehicle == null) {
            throw new Exception("车牌号还没有录入，无法绑定该车牌。");
        }

        RelateDeviceVehicle relate = new RelateDeviceVehicle();
        relate.setDevId(devId);
        relate.setDevType(device.getDevType());
        relate.setPlateNo(plateNo);
        relate.setBindTime(new Date());
        relate.setBindUser(ShiroUtils.getLoginName());
        return relateDeviceVehicleMapper.insertRelateDeviceVehicle(relate);
    }

    @Override
    public int deleteBindRelation(String devId) {
        return relateDeviceVehicleMapper.deleteRelateByDevId(devId);
    }
}
